﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Security.Cryptography;
using QuanLyHocPhi.Entity;



namespace QuanLyHocPhi.DAL
{
    public class TAI_KHOAN_DAO
    {
        private const string SQL_LOGIN = "select * from TAI_KHOAN where TEN_NGUOI_DUNG = @TEN_NGUOI_DUNG and MAT_KHAU = @MAT_KHAU and TRANG_THAI <> 1";
        

        // Parameterser
        private const string PARM_TEN_NGUOI_DUNG = "@TEN_NGUOI_DUNG";
        private const string PARM_MAT_KHAU = "@MAT_KHAU";

        private string GetMd5Hash(MD5 md5Hash, string input)
        {

            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }


        /// <summary>
        /// Login, if login success return TAI_KHOAN entity, otherwise return null
        /// </summary>
        /// <param name="TEN_NGUOI_DUNG"></param>
        /// <param name="MAT_KHAU"></param>
        /// <returns></returns>
        public TAI_KHOAN Login(string TEN_NGUOI_DUNG, string MAT_KHAU) {
            TAI_KHOAN account = null;
            SqlParameter[] parms;
            string MAT_KHAUHash = "";
            using (MD5 md5Hash = MD5.Create())
            {
                MAT_KHAUHash = GetMd5Hash(md5Hash, MAT_KHAU);
            }



            parms = new SqlParameter[] {
											 new SqlParameter(PARM_TEN_NGUOI_DUNG, SqlDbType.NVarChar),
											 new SqlParameter(PARM_MAT_KHAU, SqlDbType.NVarChar)};
            parms[0].Value = TEN_NGUOI_DUNG;
            parms[1].Value = MAT_KHAUHash;
            using (SqlDataReader reader = SQLDBHelper.ExecuteReader(SQLDBHelper.CONN_STRING,
                CommandType.Text, SQL_LOGIN, parms))
            {
                if (reader.Read())
                {
                    account = new TAI_KHOAN();
                    account.MA_TAI_KHOAN = reader.GetInt32(reader.GetOrdinal("MA_TAI_KHOAN"));
                    account.MA_VAI_TRO = reader.GetInt32(reader.GetOrdinal("MA_VAI_TRO"));
                    account.TEN_NGUOI_DUNG = reader.GetString(reader.GetOrdinal("TEN_NGUOI_DUNG"));
                    account.MAT_KHAU = reader.GetString(reader.GetOrdinal("MAT_KHAU"));
                    account.TRANG_THAI = reader.GetBoolean(reader.GetOrdinal("TRANG_THAI"));

                }
            }
            return account;
        }
    }
}
